package com.hbnu.service.impl;

import com.hbnu.mapper.CartMapper;
import com.hbnu.mapper.OrderMapper;
import com.hbnu.pojo.*;
import com.hbnu.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public BigDecimal getTolPrice(Request rq) {
        List<Collection> selectionList=rq.getSelectionList();
        BigDecimal tolPrice=new BigDecimal(0);

        for(Collection selection:selectionList){
            BigDecimal price=selection.getTolPrice();
            tolPrice=tolPrice.add(price);
        }
        return tolPrice;
    }

    @Override
    public Order previewOrder(Request rq) {
        Order order=new Order();
        BigDecimal discount=new BigDecimal("0.00");
        BigDecimal tolPrice=getTolPrice(rq);
        BigDecimal payment=tolPrice.subtract(discount);
        order.setDiscount(discount);
        order.setPayment(payment);
        order.setTolPrice(tolPrice);
        return order;
    }

    @Override
    public String createOrder(Request rq) {
        Order order=new Order();
        BigDecimal discount=new BigDecimal("0.00");
        BigDecimal tolPrice=getTolPrice(rq);
        BigDecimal payment=tolPrice.subtract(discount);
        List<Collection> selectionList=rq.getSelectionList();

//        for(Collection selection:selectionList) {
//            orderMapper.updateGoodNum(selection.getGid(), selection.getCount());
//        }
        List<Order>orderList=orderMapper.getOrderList();
        String max_id=new String("00000000000000");
        for(Order o:orderList){
            if(max_id.compareTo(o.getOid())<0)
                max_id=o.getOid();
        }
        String oid=String.format("%0"+14+"d",Long.parseLong(max_id)+1);
        LocalDateTime time=LocalDateTime.now();
        orderMapper.insertOrderDetail(oid,1,time,payment,rq.getUser().getAddress(),discount,tolPrice);
        orderMapper.insertOrderUser(oid,rq.getUser().getUid());

        for(Collection selection:selectionList){
            orderMapper.deleteCart(rq.getUser().getUid(), selection.getGid());
            orderMapper.insertOrderGoods(oid,selection.getGid(),selection.getCount());
        }
        return oid;
    }

    public Order getOrderDetail(Request rq){
        Order order=orderMapper.getOrderDetail(rq.getOid());
        List<Collection>goodsList=orderMapper.getGoodsListByOid(rq.getOid());
        goodsList.stream().forEach(goods->{
        goods.setGood(orderMapper.getGoodByGid(goods.getGid()));
        BigDecimal price=new BigDecimal(goods.getGood().getPrice());
        price=price.multiply(new BigDecimal(goods.getCount()));
        goods.setTolPrice(price);
        });
        order.setGoodsList(goodsList);
        return order;
    }
    @Override
    public List<Order> getOrderList() {
        List<String> oidList=orderMapper.getOidList();
        List<Order> orderList=new ArrayList<>();
        oidList.stream().forEach(oid->{
            Order order=orderMapper.getOrderDetail(oid);
            LocalDateTime curTime=LocalDateTime.now();
            long second= ChronoUnit.SECONDS.between( order.getTime(),curTime);
            if(second>3600&&order.getState()==1) {
                order.setState(2);
                orderMapper.updateOrderState(oid, 2);
            }
            List<Collection>goodsList=orderMapper.getGoodsListByOid(oid);
            goodsList.stream().forEach(goods->{
                goods.setGood(orderMapper.getGoodByGid(goods.getGid()));
                BigDecimal price=new BigDecimal(goods.getGood().getPrice());
                price=price.multiply(new BigDecimal(goods.getCount()));
                goods.setTolPrice(price);
            });
            order.setGoodsList(goodsList);

            orderList.add(order);
        });
        return orderList;
    }

    @Override
    public List<Order> getOrderListByContent(Request rq) {
        List<String> oidList=orderMapper.getOidListByContent(rq.getContent());
        List<Order> orderList=new ArrayList<>();
        oidList.stream().forEach(oid->{
            Order order=orderMapper.getOrderDetail(oid);
            LocalDateTime curTime=LocalDateTime.now();
            long second= ChronoUnit.SECONDS.between( order.getTime(),curTime);
            if(second>3600&&order.getState()==1) {
                order.setState(2);
                orderMapper.updateOrderState(oid, 2);
            }
            List<Collection>goodsList=orderMapper.getGoodsListByOid(oid);
            goodsList.stream().forEach(goods->{
                goods.setGood(orderMapper.getGoodByGid(goods.getGid()));
                BigDecimal price=new BigDecimal(goods.getGood().getPrice());
                price=price.multiply(new BigDecimal(goods.getCount()));
                goods.setTolPrice(price);
            });
            order.setGoodsList(goodsList);

            orderList.add(order);
        });
        return orderList;
    }

    @Override
    public List<Order> getOrderListByUid(Request rq) {
        List<String> oidList=orderMapper.getOrderListByUid(rq.getUid());
        List<Order> orderList=new ArrayList<>();
        oidList.stream().forEach(oid->{
            Order order=orderMapper.getOrderDetail(oid);
            Refund refund=orderMapper.getRefundByOid(oid);
            if(refund!=null)order.setRefund(refund);
            LocalDateTime curTime=LocalDateTime.now();
            long second= ChronoUnit.SECONDS.between( order.getTime(),curTime);
            if(second>3600&&order.getState()==1) {
                order.setState(2);
                orderMapper.updateOrderState(oid, 2);
            }
            List<Collection>goodsList=orderMapper.getGoodsListByOid(oid);
            goodsList.stream().forEach(goods->{
                goods.setGood(orderMapper.getGoodByGid(goods.getGid()));
                BigDecimal price=new BigDecimal(goods.getGood().getPrice());
                price=price.multiply(new BigDecimal(goods.getCount()));
                goods.setTolPrice(price);
            });
            order.setGoodsList(goodsList);
            orderList.add(order);
        });
        return orderList;
    }

    @Override
    public List<Order> getOrderListByUidAndState(Request rq) {
        List<String> oidList=orderMapper.getOrderListByUidAndState(rq.getUid(),rq.getState());
        List<Order> orderList=new ArrayList<>();
        oidList.stream().forEach(oid->{
            Order order=orderMapper.getOrderDetail(oid);
            Refund refund=orderMapper.getRefundByOid(oid);
            if(refund!=null)order.setRefund(refund);
            LocalDateTime curTime=LocalDateTime.now();
            long second= ChronoUnit.SECONDS.between( order.getTime(),curTime);
            if(second>3600&&order.getState()==1) {
                order.setState(2);
                orderMapper.updateOrderState(oid, 2);
            }
            List<Collection>goodsList=orderMapper.getGoodsListByOid(oid);
            goodsList.stream().forEach(goods->{
                goods.setGood(orderMapper.getGoodByGid(goods.getGid()));
                BigDecimal price=new BigDecimal(goods.getGood().getPrice());
                price=price.multiply(new BigDecimal(goods.getCount()));
                goods.setTolPrice(price);
            });
            order.setGoodsList(goodsList);

            orderList.add(order);
        });
        return orderList;
    }

    @Override
    public List<Order> getOrderListByStateAndContent(Request rq) {
        List<String> oidList=orderMapper.getOrderListByStateAndContent(rq.getContent(),rq.getState());
        List<Order> orderList=new ArrayList<>();
        oidList.stream().forEach(oid->{
            Order order=orderMapper.getOrderDetail(oid);
            LocalDateTime curTime=LocalDateTime.now();
            long second= ChronoUnit.SECONDS.between( order.getTime(),curTime);
            if(second>3600&&order.getState()==1) {
                order.setState(2);
                orderMapper.updateOrderState(oid, 2);
            }
            List<Collection>goodsList=orderMapper.getGoodsListByOid(oid);
            goodsList.stream().forEach(goods->{
                goods.setGood(orderMapper.getGoodByGid(goods.getGid()));
                BigDecimal price=new BigDecimal(goods.getGood().getPrice());
                price=price.multiply(new BigDecimal(goods.getCount()));
                goods.setTolPrice(price);
            });
            order.setGoodsList(goodsList);

            orderList.add(order);
        });
        return orderList;    }

    @Override
    public List<Refund> getRefundListByContent(Request rq) {
        List<Refund> refundList=orderMapper.getRefundListByContent(rq.getContent());
        return refundList;
    }

    @Override
    public List<Refund> getRefundListByStateAndContent(Request rq) {
        List<Refund>refundList=orderMapper.getRefundListByAgreeAndContent(rq.getContent(),rq.getState());
        return refundList;
    }

    @Override
    public void updateOrderAddress(Request rq) {
        orderMapper.updateOrderAddress(rq.getOid(),rq.getAddress());
    }

    @Override
    public void paySuccessful(Request rq) {
        orderMapper.updateOrderState(rq.getOid(),3);
    }

    @Override
    public void reOrder(Request rq) {
        LocalDateTime curTime=LocalDateTime.now();
        orderMapper.updateOrderTime(rq.getOid(),curTime);
        orderMapper.updateOrderState(rq.getOid(),1);
        List<Collection>selectionList=orderMapper.getGoodsListByOid(rq.getOid());
        for(Collection selection:selectionList) {
            orderMapper.updateGoodNum(selection.getGid(), selection.getCount());
        }
    }

    @Override
    public void refund(Request rq) {
        try{
            orderMapper.insertRefund(rq.getOid(),rq.getUid(),rq.getContent(),0);
        }catch (Exception e){
            orderMapper.updateRefund(0,rq.getOid());
            orderMapper.updateRefundReason(rq.getContent(),rq.getOid());
        }
    }
    @Override
    public int deliverGood(Request rq) {
        try{
            orderMapper.updateOrderState(rq.getOid(),4);

            List<Order> orderList=orderMapper.getOrderList();
            String max_id="000000000000000";
            for(Order order:orderList){
                if(order.getEid()==null)continue;
                if(max_id.compareTo(order.getEid())<0)max_id=order.getEid();
            }
            String eid=String.format("%0"+15+"d",Long.parseLong(max_id)+1);
            orderMapper.updateOrderEidAndCompany(rq.getOid(),eid,"顺丰快递");
            return 1;
        }catch (Exception e){
            return 0;
        }

    }

    @Override
    public void refuteRefund(Request rq) {
        orderMapper.updateRefund(2,rq.getOid());
    }

    @Override
    public void agreeRefund(Request rq) {
        orderMapper.updateOrderState(rq.getOid(),7);
        orderMapper.updateRefund(1, rq.getOid());
    }

    @Override
    public int getRefundAgree(Request rq) {
        Refund refund=orderMapper.getRefundByOid(rq.getOid());
        return refund.getAgree();
    }
}
